﻿using System;
using System.Drawing;
using RRRSRoguelike.Helpers;
using RRRSRoguelike.Helpers.Visualizer;
using RRRSRoguelike.Managers;

namespace RRRSRoguelike.Screens
{
	/// <summary>
	/// Controls game menu.  This class adds additional screens to the main stack based on user options. 
	/// </summary>
	class MenuScreen : Screen
	{
		private readonly Menu menu;

		public MenuScreen()
		{
			Point startPosition = new Point(4,3);
			Point border = new Point(9,6); //this is border X2
			//Setup one panel to fill screen
			PanelList.Add(new Panel(startPosition,
				Visualizer.GetScreenWidth() - border.X,
				Visualizer.GetScreenHeight() - border.Y));
			
			//instantiate the menu	
			menu = new Menu(PanelList[0]);
			
			SetupMenu();
		}

		void SetupMenu()
		{
			//Add the menu items
			menu.Add(new MenuItem(Properties.Strings.StartGame, SelectNewGame));
			menu.Add(new MenuItem(Properties.Strings.EnterSeed, SelectSeed));
			menu.Add(new MenuItem("High scores", SelectViewHighScores));
			menu.Add(new MenuItem("Options", SelectOptions));
			menu.Add(new MenuItem(Properties.Strings.Exit, SelectExit));
		}

		#region Methods

		public override void Show()
		{
			Draw();
			//The menthods below will set active to false when a selection is made.
			while (active)
			{
				//handle menu input and draw
				if (menu.HandleInput())
					menu.DrawMenu();
			}
		}

		void Draw()
		{
			Clear();
			PanelList[0].Draw();
			menu.DrawMenu();
		}
		#endregion
		
		#region delegate
		//Event method
		void SelectNewGame(object sender, EventArgs e)
		{
            SeedHelper.SetSeed();
            ApplicationManager.Instance.ScreenStack.Push(new GameScreen());
			active = false;
		}
		//Event method
		void SelectSeed(object sender, EventArgs e)
		{
			ApplicationManager.Instance.ScreenStack.Push(new SeedScreen());
			active = false;
		}

		//Event method
		void SelectViewHighScores(object sender, EventArgs e)
		{
			ApplicationManager.Instance.ScreenStack.Push(new MenuScreen());
			ApplicationManager.Instance.ScreenStack.Push(new HighScoreScreen());
			active = false;
		}
		
		//Event method
		void SelectOptions(object sender, EventArgs e)
		{
			ApplicationManager.Instance.ScreenStack.Push(new OptionsScreen());
			active = false;
		}
		//Event method
		void SelectExit(object sender, EventArgs e)
		{
			ApplicationManager.Instance.Exit();
		}

		#endregion
	}
}
